package com.zhonglz.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhonglz.commons.base.BaseController;
import com.zhonglz.model.BuSeal;
import com.zhonglz.model.SysFileInfo;
import com.zhonglz.util.minio.MinioProperties;
import com.zhonglz.util.result.ResultUtils;
import com.zhonglz.util.tools.DataGrid;
import com.zhonglz.util.tools.ObjectUtils;
import com.zhonglz.util.tools.RandomUtils;

/***
 * 印章接口
 * @author xiaozhong
 *
 */
@RestController
@RequestMapping("/sys/seal")
public class SealController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(SealController.class);
	
	/***
     * 印章列表信息
     * @param sealName 印章名称
     * @param sealType 印章类型
     */
    @ResponseBody
    @RequestMapping("selectListByData")
    public DataGrid selectListByData(BuSeal buSeal,
    		@RequestParam(defaultValue="") String startTime,
    		@RequestParam(defaultValue="") String endTime,
    		@RequestParam(defaultValue = "1") int pageNumber,
			@RequestParam(defaultValue = "10") int pageSize,
			HttpServletRequest req) {
    	DataGrid dataGrid = null;
		// 封装查询条件参数
		Map<String, Object> mapParam = new HashMap<String, Object>();
		if(ObjectUtils.isNotEmpty(startTime)){
			startTime+=" 00:00:00";
			mapParam.put("startTime",startTime);
		}
		if(ObjectUtils.isNotEmpty(endTime)){
			endTime+=" 23:59:59";
			mapParam.put("endTime",endTime);
		}
		if(ObjectUtils.isNotEmpty(buSeal)){
			if(ObjectUtils.isNotEmpty(buSeal.getSealName())){
				mapParam.put("sealName",buSeal.getSealName());
			}
		}
		PageHelper.startPage(pageNumber, pageSize);
		List<BuSeal> selectBuSealList=buSealService.selectByDataList(mapParam);
		PageInfo<BuSeal> info = new PageInfo<BuSeal>(selectBuSealList);
		
		List<BuSeal> retList = new ArrayList<>();
		for (BuSeal buSealInfo: selectBuSealList) {
			SysFileInfo infos =sysFileInfoService.selectByPrimaryKey(buSealInfo.getSealFile());
			String path = MinioProperties.endpoint+"/"+MinioProperties.bucketName+"/"+infos.getFileAddress();
			buSealInfo.setSealImgUrl(path);
			retList.add(buSealInfo);
		}
		// 封装结果集
		dataGrid = new DataGrid(info.getTotal(),retList,info.getPages(),info.getPageNum(),ResultUtils.CODE_SUCCESS);
    	return dataGrid; 
	}
    
    @ResponseBody
    @RequestMapping("selectInfoById")
    public BuSeal selectInfoById(String id) {
    	return buSealService.selectById(id);
    }
    
    /***
     * 保存印章
     * @param fileName 文件名称
     * @param sealName 印章名称
     * @param sealType 印章类型 1.模板章 2.实体章
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/createSeal", method = RequestMethod.POST)
    public ResultUtils createSeal(String sealFile, String sealName,String sealNumber, String sealType){
        if (ObjectUtils.isEmpty(sealFile)){
            return new ResultUtils(ResultUtils.CODE_ERROR, "文件Uid为空!");
        }
        if (ObjectUtils.isEmpty(sealName)){
            return new ResultUtils(ResultUtils.CODE_ERROR, "印章名称为空!");
        }
        if (ObjectUtils.isEmpty(sealNumber)){
            return new ResultUtils(ResultUtils.CODE_ERROR, "印章编号为空!");
        }
        
        if (ObjectUtils.isEmpty(sealType)){
            return new ResultUtils(ResultUtils.CODE_ERROR, "印章类型为空!");
        }
        return buSealService.createSeal(sealFile, sealName,sealNumber, sealType);
    }
    
    
	/***
     * 生成临时模板印章
     * @param sealName 印章名称
     * @param sealType 印章类型
     */
    @ResponseBody
    @RequestMapping(value = "/createTempSeal", method = RequestMethod.POST)
    public ResultUtils createTempSeal(String sealName, Integer sealType){
        if (ObjectUtils.isEmpty(sealName)){
            return new ResultUtils(ResultUtils.CODE_ERROR, "印章名称为空!");
        }
        if (ObjectUtils.isEmpty(sealType)){
            return new ResultUtils(ResultUtils.CODE_ERROR, "印章类型为空!");
        }
        return buSealService.createTempSeal(sealName, sealType);
    }
    
    /***
     * 印章编号生成
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/createSealNumber", method = RequestMethod.POST)
    public ResultUtils createSealNumber(){
    	String sealNumber =RandomUtils.getSealNo();
        return new ResultUtils(ResultUtils.CODE_SUCCESS,"编号生成成功！", sealNumber);
    }
    
    
    
}
